<?php namespace App\Http\Controllers\Index;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\ItemService;
use App\Models\Item;
use App\Models\Catalog;
use App\Models\Area;

class ItemController extends Controller
{

    public function items(ItemService $itemService, Request $request)
    {
        return view('index.item.items', [
            'types' => Item::$_typeName,
            'p_catalogs' => Catalog::where(['p_id' => 0, 'level' => 1])->get(),
            'catalogs' => Catalog::where(['p_id' => $request->input('p_catalog_id'), 'level' => 2])->get(),
            'provinces' => Area::where(['p_id' => 0, 'level' => 1])->get(),
            'cities' => Area::where(['p_id' => $request->input('province_id'), 'level' => 2])->get(),
            'districts' => Area::where(['p_id' => $request->input('city_id'), 'level' => 3])->get(),
            'items' => $itemService->getList($request)
        ])->withInput($request->all());
    }

    public function item($id)
    {
        if ($item = Item::find($id)) {
            return view('index.item.item', [
                'item' => $item,
                'lostItems' => Item::where('type', 'lost')->orderby('id', 'desc')->take(10)->get(),
                'findItems' => Item::where('type', 'find')->orderby('id', 'desc')->take(10)->get()
            ]);
        }
        return redirect()->back();
    }
}
